let checkNetWork = require("../CheckNetWork.js");
var tcity = require("../../utils/citys.js");


let app = getApp();
Page({
  data: {
    provinces: [],
    province: "",
    citys: [],
    city: "",
    countys: [],
    county: '',
    value: [0, 0, 0],
    values: [0, 0, 0],
    condition: false,
    industry: [{ code: 0, display: '农业' }, { code: 1, display: '工业' },{ code: 3, display: '服务业' }],
    industryDisplay:'',
    focus: false,
    btnDisabled: true,
    getCodeBtnProperty: {
      titileColor: '#B4B4B4',
      disabled: true,
      loading: false,
      title: '获取验证码'
    },
    getCodeParams: {
      account: '',
      action: 'reg',
    },
    Profile: {
      phone: '',
      verifyCode: '',
      realname: '',
      company:'',
      position: '',
      industry: '',
      address: ''
    },
    openid:''
  },
  bindChange: function (e) {
    console.log(e);
    var val = e.detail.value
    var t = this.data.values;
    var cityData = this.data.cityData;

    if (val[0] != t[0]) {
      console.log('province no ');
      const citys = [];
      const countys = [];

      for (let i = 0; i < cityData[val[0]].sub.length; i++) {
        citys.push(cityData[val[0]].sub[i])
      }
      for (let i = 0; i < cityData[val[0]].sub[0].sub.length; i++) {
        countys.push(cityData[val[0]].sub[0].sub[i])
      }

      this.setData({
        province: this.data.provinces[val[0]],
        city: cityData[val[0]].sub[0],
        citys: citys,
        county: cityData[val[0]].sub[0].sub[0],
        countys: countys,
        values: val,
        value: [val[0], 0, 0]
      })
      console.log(this.data)

      return;
    }
    if (val[1] != t[1]) {
      console.log('city no');
      const countys = [];

      for (let i = 0; i < cityData[val[0]].sub[val[1]].sub.length; i++) {
        countys.push(cityData[val[0]].sub[val[1]].sub[i])
      }

      this.setData({
        city: this.data.citys[val[1]],
        county: cityData[val[0]].sub[val[1]].sub[0],
        countys: countys,
        values: val,
        value: [val[0], val[1], 0]
      })
      return;
    }
    if (val[2] != t[2]) {
      console.log('county no');
      this.setData({
        county: this.data.countys[val[2]],
        values: val
      })
      return;
    }


  },
  open:function () {
    console.log(this.data.province)
    this.setData({
      condition: !this.data.condition
    })
  },
  industrySelect: function (e) {
    let tmp = this.data.industry[e.detail.value]
    this.setData({
      'Profile.industry': tmp.code,
      industryDisplay: tmp.display,
      btnDisabled: false
    });
  },
  onLoad:function(options){
    console.log(options.openid)
    var that = this;
    tcity.init(that);
    var cityData = that.data.cityData;
    const provinces = [];
    const citys = [];
    const countys = [];

    for (let i = 0; i < cityData.length; i++) {
      provinces.push(cityData[i]);
    }
    console.log('省份完成');
    for (let i = 0; i < cityData[0].sub.length; i++) {
      citys.push(cityData[0].sub[i])
    }
    console.log('city完成');
    for (let i = 0; i < cityData[0].sub[0].sub.length; i++) {
      countys.push(cityData[0].sub[0].sub[i])
    }

    that.setData({
      'openid': options.openid,
      'provinces': provinces,
      'citys': citys,
      'countys': countys,
      'province': cityData[0],
      'city': cityData[0].sub[0],
      'county': cityData[0].sub[0].sub[0]
    })
    console.log('初始化完成');
  },
  userInput: function (e) {
    //手机号输入
    let that = this;
    let inputValue = e.detail.value;
    let length = e.detail.value.length;
    if ((/^1[34578]\d{9}$/.test(inputValue))) {
      that.setData({
        'Profile.phone': inputValue,
        'getCodeBtnProperty.titileColor': '#9ED99D',
        'getCodeBtnProperty.disabled': false
      })

    } else {
      that.setData({
        'Profile.phone': '',
        'getCodeBtnProperty.titileColor': '#B4B4B4',
        'getCodeBtnProperty.disabled': true
      })
    }
  },
  placeValue:function(e){
    
    let inputValue = e.detail.value;
    let tmp = this.data.Profile;
    tmp[e.currentTarget.dataset.code] = inputValue
    console.log(tmp)
    this.setData({
      Profile: tmp
    });
    
  },
  
  //获取验证码
  getCodeAct: function () {
    //请求接口
    let that = this;
    if (checkNetWork.checkNetWorkStatu() == false) {
      console.log('网络错误')
    } else {
      console.log(this.data.Profile.phone)
      let phone = this.data.Profile.phone


      //发起网络请求  
      wx.request({
        url: app.globalData.BaseURL + '/common/verifyCode/' + phone,
        header: {
          "Content-Type": "application/json"
        },
        method: 'post',
        success: function (res) {
          console.log(res.data.msg)
          let message = res.data.msg;
          let statu = res.data.code;
          if (statu == 0) {
            wx.showToast({
              title: '验证码已发送',
            })
            //启动定时器
            var number = 60;
            var time = setInterval(function () {
              number--;
              that.setData({
                'getCodeBtnProperty.title': number + '秒',
                'getCodeBtnProperty.disabled': true
              })
              if (number == 0) {
                that.setData({
                  'getCodeBtnProperty.title': '重新获取',
                  'getCodeBtnProperty.disabled': false
                })
                clearInterval(time);
              }
            }, 1000);
          } else {
            wx.showToast({
              title: message,
              icon: 'loading',
              duration: 2000,
            })
            that.setData({
              'getCodeBtnProperty.titileColor': '#B4B4B4',
              'getCodeBtnProperty.disabled': true
            })
          }
        },
      })
      that.setData({
        'getCodeBtnProperty.loading': false
      })

    }
  },
  toastShow: function (str, icon) {
    var _this = this;
    _this.setData({
      isShow: true,
      txt: str,
      iconClass: icon
    });
    setTimeout(function () {    //toast消失
      _this.setData({
        isShow: false
      });
    }, 1500);
  },
  bindButtonTap: function () { 
    let that = this;
    var userInfo = wx.getStorageSync('userInfo');
    var user = wx.getStorageSync("user");
    //发起网络请求  
    wx.request({
      url: app.globalData.BaseURL + '/wx/ma/bind',
      header: {
        "Content-Type": "application/json"
      },
      method: 'post',
      data: {
        phone: that.data.Profile.phone,
        verifyCode: that.data.Profile.verifyCode,
        openid: that.data.openid,
        sex: user.gender,
        company: that.data.Profile.company,
        avatar: user.avatarUrl,
        position: that.data.Profile.position,
        industry: that.data.Profile.industry,
        province: that.data.province.code,
        city: that.data.city.code,
        district: that.data.county.code,
        realname: that.data.Profile.realname,
        address: that.data.Profile.address
      },
      success: function (res) {
        if (res.statusCode == 200) {
          if (res.data.code == 0) {
            wx.setStorageSync("userInfo", res.data.data);
            //跳到主页
            wx.redirectTo({
              url: '../index/index'
            })
          } else {
            wx.showToast({
              title: res.data.msg,
            })
          }
        } else {
          wx.reLaunch({
            url: '../auth/auth',
          })
        }
      },
    })
  }
})
   